package com.longrise.bjjt.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.android.util.Util;
import com.longrise.android.widget.LLinearLayoutView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.ui.AbstractSpinerAdapter.IOnItemSelectListener;
import com.longrise.bjjt.ui.AlertDialog;
import com.longrise.bjjt.ui.SpinerPopWindow;
import com.longrise.bjjt_lib.R;

import java.util.List;

public class UiUtil {

    public static final int NETTYPE_WIFI = 0x01;
    public static final int NETTYPE_CMWAP = 0x02;
    public static final int NETTYPE_CMNET = 0x03;
    public static Toast mToast1 = null;

    /**
     * 显示toast
     *
     * @param context
     * @param content
     */
    // public static void showToast(final Context context, String content)
    // {
    // Toast.makeText(context, content, Toast.LENGTH_LONG).show();
    // }
    public static void showToast(final Context context, String content) {
        if (mToast1 == null) {
            mToast1 = Toast.makeText(context, content, Toast.LENGTH_LONG);

        } else {
            mToast1.setText(content);
            mToast1.setDuration(3000);

        }

        mToast1.show();

    }

    /**
     * 显示下拉选择popwindow 可以通过isWarpContent设置是否包裹内容
     *
     * @param context       上下文
     * @param dataList      list数据
     * @param listener      选择回调函数
     * @param view          要显示在哪个view下面
     * @param isWarpConent 是否包裹内容
     */
    public static SpinerPopWindow showSelectPopWindow(Context context, List<String> dataList,
                                                      IOnItemSelectListener listener, View view, boolean isWarpConent) {
        SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(context,isWarpConent);
        mSpinerPopWindow.refreshData(dataList, 0);
        mSpinerPopWindow.setItemListener(listener);
//        mSpinerPopWindow.setWidth(view.getWidth());
        mSpinerPopWindow.setView(view);
//        mSpinerPopWindow.showAsDropDown(view);
        mSpinerPopWindow.showAtLocation(view,Gravity.CENTER,0,0);
        return mSpinerPopWindow;
    }

    /**
     * 选择对话框
     *
     * @param context    上下文
     * @param title      标题
     * @param content    内容
     * @param leftStr    左边btn内容
     * @param rightStr   右边btn内容
     * @param leftClick
     * @param rightClick
     */
    public static AlertDialog showDialog(Context context, String title, String content, String leftStr, String rightStr,
                                         OnClickListener leftClick, OnClickListener rightClick) {
        final AlertDialog dialog = new AlertDialog(context).builder();
        dialog.setTitle(title);
        dialog.setShowTitle(true);
        dialog.setShowEditText(false);
        dialog.setMsg(content);
        dialog.setNegativeButton(leftStr, leftClick);
        dialog.setPositiveButton(rightStr, rightClick);
        dialog.show();
        return dialog;
    }

    /**
     * 单按钮强制跳转的对话框
     *
     * @param context
     * @param title
     * @param content
     * @param rightClick
     */
    public static AlertDialog showSingleDialog(Context context, String title, String content, String login,
                                               OnClickListener rightClick) {
        final AlertDialog dialog = new AlertDialog(context).builder();
        dialog.setTitle(title);
        dialog.setShowTitle(true);
        dialog.setShowEditText(false);
        dialog.setLeftMsg(content);
        dialog.setPositiveButton(login, rightClick);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 确定取消提示对话框
     *
     * @param context
     * @param content
     * @param leftClick
     * @param rightClick
     */
    public static AlertDialog showDialog(Context context, String content, OnClickListener leftClick,
                                         OnClickListener rightClick) {
        return showDialog(context, "提示", content, "取消", "确定", leftClick, rightClick);
    }

    /**
     * 显示waiting对话框
     *
     * @param text
     */
    public static Dialog showProcessDialog(Context context, String text) {

        Dialog processDialog = new Dialog(context, R.style.dialog_style);
        processDialog.setCanceledOnTouchOutside(false);

        LLinearLayoutView dialogView = new LLinearLayoutView(context);
        dialogView.setBackgroundColor(Color.parseColor("#55000000"), -10, -10, -10, -10, Util.dip2px(context, 5), 0,
                Color.parseColor("#55000000"));
        dialogView.setGravity(Gravity.CENTER);
        dialogView.setOrientation(LinearLayout.VERTICAL);

        // 进度条
        ProgressBar bar = new ProgressBar(context);
        bar.setIndeterminate(true);
        bar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.lcontainerview_anim_loading));
        int w = Util.dip2px(context, 30);
        dialogView.addView(bar, w, w);

        TextView textView = new TextView(context);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(14);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        textView.setPadding(0, Util.dip2px(context, 10), 0, 0);
        dialogView.addView(textView);

        int widthDialog = Util.dip2px(context, 100);
        LayoutParams dialogLP = new LayoutParams(widthDialog, widthDialog);
        processDialog.setContentView(dialogView, dialogLP);
        // add by huxin
        processDialog.show();
        //

        dialogLP = null;

        return processDialog;
    }

    // Toast mToast = null;
    //
    // /** 安全toast，并且多次点击只弹出一次 */
    // public void showSafeToast(final Context context, final String msg)
    // {
    //
    // if ("main".equals(Thread.currentThread().getName()))
    // {
    //
    // if (mToast == null)
    // {
    // mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    // mToast.setGravity(Gravity.CENTER, 0, 0);
    // } else
    // {
    // mToast.setText(msg);
    // mToast.setDuration(3000);
    // mToast.setGravity(Gravity.CENTER, 0, 0);
    // }
    //
    // mToast.show();
    //
    // } else
    // {
    // ((Activity) context).runOnUiThread(new Runnable()
    // {
    // @Override
    // public void run()
    // {
    //
    // if (mToast == null)
    // {
    // mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    // mToast.setGravity(Gravity.CENTER, 0, 0);
    // } else
    // {
    // mToast.setText(msg);
    // mToast.setDuration(Toast.LENGTH_LONG);
    // mToast.setGravity(Gravity.CENTER, 0, 0);
    // }
    //
    // mToast.show();
    // }
    // });
    // }
    //
    // }

    /**
     * 得到上下文
     */
    public static Context getContext() {
        return BaseApplication.getContext();
    }

    /**
     * 得到Resouce对象
     */
    public static Resources getResource() {
        return getContext().getResources();
    }

    /**
     * 得到应用程序的包名
     */
    public static String getPackageName() {
        return getContext().getPackageName();
    }

    /**
     * dip-->px
     */
    public static int dip2Px(int dip) {
        // px/dip = density;
        float density = getResource().getDisplayMetrics().density;
        int px = (int) (dip * density + .5f);
        return px;
    }

    /**
     * px-->dip
     */
    public static int px2Dip(int px) {
        // px/dip = density;
        float density = getResource().getDisplayMetrics().density;
        int dip = (int) (px / density + .5f);
        return dip;
    }
}
